// Unique ID creation requires a high quality random # generator.  In node.js
// this is pretty straight-forward - we use the crypto API.

//var crypto = require('crypto');
const CryptoJS = require('../../CryptoJS/crypto-js.js');

//十六进制字符串转字节数组
function Str2Bytes(str)
{
    var pos = 0;
    var len = str.length;
    if(len %2 != 0)
    {
       return null; 
    }
    len /= 2;
    var hexA = new Array();
    for(var i=0; i<len; i++)
    {
       var s = str.substr(pos, 2);
       var v = parseInt(s, 16);
       hexA.push(v);
       pos += 2;
    }
    return hexA;
}
 
 
 
//字节数组转十六进制字符串
function Bytes2Str(arr)
{
    var str = "";
    for(var i=0; i<arr.length; i++)
    {
       var tmp = arr[i].toString(16);
       if(tmp.length == 1)
       {
           tmp = "0" + tmp;
       }
       str += tmp;
    }
    return str;
}

module.exports = function nodeRNG() {

	var wordArray = CryptoJS.lib.WordArray.random(16);

	var hexString = CryptoJS.enc.Hex.stringify(wordArray);

	var hexArray = Str2Bytes(hexString);

	return hexArray;
};
